<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '新增人员' : '编辑人员'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px" top="4.2vh">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入姓名" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色：" prop="role">
              <el-select v-model="ruleForm.role" placeholder="请选择角色" clearable>
                <el-option v-for="item in roleArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话：" prop="tel">
              <el-input v-model="ruleForm.tel" placeholder="请输入电话" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工号：" prop="no">
              <el-input v-model="ruleForm.no" placeholder="请输入工号" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属科室：" prop="area_id">
              <el-select v-model="ruleForm.area_id" placeholder="请选择所属科室" clearable>
                <el-option v-for="item in deptArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="NFC卡号：" prop="nfc">
              <el-input v-model="ruleForm.nfc" placeholder="请输入NFC卡号" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="限制数量：" prop="maximum">
              <el-input-number v-model="ruleForm.maximum" placeholder="请输入限制工服数量" :precision="0" :step="1" :max="9999" :min="0"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="工服属性：" prop="classify">
              <el-select v-model="ruleForm.classify" placeholder="请选择工服属性" clearable multiple collapse-tags>
                <el-option v-for="item in clothesClassifyArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row style="height: 19.3rem;overflow: auto;">
          <el-col :span="24" v-for="(item, index) in clothesClassifyArray" :key="item.id">
            <el-form-item :label="item.name + '：'" prop="properties">
              <el-checkbox-group v-model="selectedValues">
                <div class="oneBox">
                  <div v-for="(itema, index) in item.child" :key="itema.id" class="twoClass">
                    <div class="twocheckbox">
                      <el-checkbox @change="handleChecked1()" :label="itema.id" name="type" :value="itema.id">{{ itema.name }}</el-checkbox>
                    </div>
                    <div v-if="selectedValues" class="twoBox">
                      <div v-for="itemb in itema.child" :key="itemb.id" class="threeClass">
                        <div class="trecheckbox">
                          <el-checkbox v-model="itemb.id" :label="itemb.id" name="type" :value="itemb.id" @change="handleChecked2(itemb)">{{ itemb.name }}</el-checkbox>
                        </div>
                        <div v-if="itemb.child" class="threeBox">
                          <div v-for="itemc in itemb.child" :key="itemc.id">
                            <div class="fourcheckbox">
                              <el-checkbox v-model="itemc.id" :label="itemc.id" name="type" :value="itemc.id" @change="handleChecked3(itemc)">{{ itemc.name }}</el-checkbox>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row> -->
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="" prop="apply">
              <el-checkbox v-model="ruleForm.apply" :true-label="1" :false-label="0">允许取公用织物</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">关 闭</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getRole, getClothClassifyDoctor } from "@/api/system";
import { getEmployeeInfo, getArea, employeeAdd, employeeEdit } from "@/api/basic";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        name: "",
        tel: "",
        no: "",
        nfc: "",
        role: "",
        area_id: "",
        classify: [],
        maximum: 0,
      },
      rules: {
        // 表单规则
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        role: [{ required: false, message: "请选择角色", trigger: "change" }],
        tel: [{ required: true, message: "请输入电话", trigger: "blur" }],
        nfc: [{ required: false, message: "请输入NFC卡号", trigger: "blur" }],
        no: [{ required: true, message: "请输入工号", trigger: "blur" }],
        area_id: [{ required: true, message: "请选择所属科室", trigger: "change" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      roleArray: [], // 角色集合
      regionArray: [], // 院区集合
      deptArray: [], // 科室集合
      clothesClassifyArray: [], // 衣物分类集合
      selectedValues: [], // 选中的衣物分类集合
      level1Checked: false,
      level2Checked: false,
      selectedLevel1: [],
      selectedChildren: {},
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        // 获取科室集合
        this.getDeptList();

       // 获取工服属性集合
       this.getClothClassifyDoctorList();

       // 获取角色集合
       this.getRoleList();
      }
    },
    selectedValues(val){
      // console.log("asdaw",val);
      this.ruleForm.properties = val;
    }
  },
  // computed: {
  //   isFirstChildSelected() {
  //     return this.selectedLevel1.length > 0 && this.selectedLevel1[0] === this.options.level1[0].value;
  //   },
  // },
  methods: {
    // 详情
    getDetail() {
      getEmployeeInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          // this.selectedValues = res.data.properties;
          this.ruleForm = res.data;
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.id == "") {
           employeeAdd(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
             }
          });
        } else {
          employeeEdit(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
        }
      });
    },
    // 获取角色集合
    getRoleList(){
      getRole({}).then((res) => {
        if (res.code == 200) {
          this.roleArray = res.data;
        }
      });
    },
    // 获取科室集合
    getDeptList(){
      getArea().then((res) => {
        if (res.code == 200) {
          this.deptArray = res.data;
        }
      });
    },
    // 获取工服属性集合
   getClothClassifyDoctorList(){
      getClothClassifyDoctor({}).then((res) => {
        if (res.code == 200) {
          this.clothesClassifyArray = res.data;
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}
.twoClass {
  display: flex;
  align-items: center;
  border: 1px solid #ebeef5;
}
.oneBox {
  margin-left: 30px;
  // height: 15.62rem;
  // overflow: auto;
}
/deep/.el-form-item__content {
  line-height: 10px;
}
.twoBox {
  margin-left: 50px;
  display: flex;
  flex-direction: column;
}
.threeClass {
  display: flex;
  align-items: center;
  // border: 1px solid #ebeef5;
}
.twocheckbox {
  width: 80px;
  display: flex;
  flex-direction: column;
  margin-left: 20px;
}
.threeBox {
  margin-left: 50px;
  margin-right: 30px;
  // margin-bottom: 10px;
}
.trecheckbox {
  margin-left: 20px;
  margin-right: 20px;
  // display: flex;
  // flex-direction: column;
}
</style>
